package org.example;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws Throwable {
        System.out.println("Hello World!");
        String file = "D:\\scy\\my.tar.gz";

        try (InputStream fileIn = new FileInputStream(file);
             InputStream gzipIn = new GZIPInputStream(fileIn);
             TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {

            TarArchiveEntry entry;
            while ((entry = tarIn.getNextTarEntry()) != null) {
                if (entry.isDirectory()) continue;

                System.out.println("读取文件: " + entry.getName() + ", 大小: " + entry.getSize());

                // 示例：读取内容到字符串（假设是文本文件）
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[4096];
                int len;
                while ((len = tarIn.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }

                String content = baos.toString("UTF-8"); // 假设是 UTF-8 编码
                System.out.println("内容预览:\n" + content.substring(0, Math.min(200, content.length())) + "\n------");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
